package view.component.monster.large_monster
{
	import flash.text.TextFormat;
	
	import enums.main.UISize;
	import enums.monster.LMonsterDetailNavigatorScreenName;
	
	import feathers.controls.Radio;
	import feathers.controls.Screen;
	import feathers.controls.ScreenNavigator;
	import feathers.controls.ScreenNavigatorItem;
	import feathers.core.ToggleGroup;
	
	import starling.display.Image;
	import starling.events.Event;
	import starling.textures.Texture;
	
	import util.AtlasTextureGenerator;
	import util.FG2FUGenerator;
	
	import view.event.monster.LMonsterDetailViewEvent;
	import view.transition.AnimationNaviTransitionManager;
	
	
	/**
	 * 大型怪物详细信息界面
	 * @author £iang
	 * 
	 */	
	public class LMonsterDetailView extends Screen
	{
		//-----------------------------
		// variables
		//-----------------------------
		
		/** 大型怪物信息切换按钮组 */
		public var buttonVec:Vector.<Radio>;
		/** 大型怪物详细信息导航栏 */
		public var detailInfoNavigator:ScreenNavigator;
		
		//-----------------------------
		// properties
		//-----------------------------
		
		/** tabBar列数 */
		private var _tabColumnNum:int;
		/** tabBar间隔 */
		private var _tabGap:Number;
		/** 单选组 */
		private var _toggleGroup:ToggleGroup;
		/** 转场管理 */
		private var _transitManager:AnimationNaviTransitionManager;
		
		//-----------------------------
		// setter&getter
		//-----------------------------
		
		//-----------------------------
		// methods
		//-----------------------------
		
		/**
		 * 初始化函数 
		 * 
		 */		
		override protected function initialize():void
		{
			var labelVec:Vector.<String> = new Vector.<String>;
			
			labelVec.push(
				"简介", "肉质", "特殊攻击效果", "道具效果", "剥取&掉落", 
				"部位破坏效果", "捕获报酬", "出现任务", "体型", 
				"攻击方式", "视频收藏", "返回怪物列表"
			);
			
			buttonVec = new Vector.<Radio>();
			_toggleGroup = new ToggleGroup();
			
			var len:int = labelVec.length;
			var tmpButton:Radio;
			var iconVec:Vector.<Texture> = AtlasTextureGenerator.getRandomCatTextures(
				"head", len );
			var tmpImage:Image;
			
			for ( var i:int = 0; i < len; i ++ )
			{
				tmpButton = FG2FUGenerator.generateRoundRectRadio(
					UISize.LMONSTER_DETAIL_TBUTTON_W, UISize.TAB_BUTTON_H, 
					this.dpiScale, UISize.TAB_BUTTON_H * 0.9, "small"
				);
				tmpButton.label = labelVec[ i ];
				tmpImage = new Image( iconVec[ i ] );
				tmpImage.scaleX = tmpImage.scaleY = this.dpiScale;
				tmpButton.defaultSelectedIcon = tmpImage;
				tmpButton.selectedHoverLabelProperties.textFormat = new TextFormat();
				tmpButton.gap = -10 * this.dpiScale;
				tmpButton.iconPosition = "right";
				tmpButton.toggleGroup = _toggleGroup;
				buttonVec.push( tmpButton );
				this.addChild( buttonVec[ i ] );
			}
			
			this.detailInfoNavigator = new ScreenNavigator();
			this.addChild( detailInfoNavigator );
			this.detailInfoNavigator.addScreen(
				LMonsterDetailNavigatorScreenName.NONE,
				new ScreenNavigatorItem( Screen )
			);
			
			this.detailInfoNavigator.addScreen(
				LMonsterDetailNavigatorScreenName.INTRODUCTION,
				new ScreenNavigatorItem( LMDetailIntroductionView )
			);
			
			this.detailInfoNavigator.addScreen(
				LMonsterDetailNavigatorScreenName.MEAT_QUALITY,
				new ScreenNavigatorItem( LMDetailMeatQualityView )
			);
			
			this.detailInfoNavigator.addScreen(
				LMonsterDetailNavigatorScreenName.SP_EFFECT,
				new ScreenNavigatorItem( LMDetailSpEffectView )
			);
			
			this.detailInfoNavigator.addScreen(
				LMonsterDetailNavigatorScreenName.ITEM_EFFECT,
				new ScreenNavigatorItem( LMDetailItemEffectView )
			);
			
			this.detailInfoNavigator.addScreen(
				LMonsterDetailNavigatorScreenName.COLLECT,
				new ScreenNavigatorItem( LMDetailCollectView )
			);
			
			this.detailInfoNavigator.addScreen(
				LMonsterDetailNavigatorScreenName.DESTROY,
				new ScreenNavigatorItem( LMDetailDestroyView )
			);
			
			this.detailInfoNavigator.addScreen(
				LMonsterDetailNavigatorScreenName.CATCH,
				new ScreenNavigatorItem( LMDetailCatchView )
			);
			
			this.detailInfoNavigator.addScreen(
				LMonsterDetailNavigatorScreenName.APPEAR,
				new ScreenNavigatorItem( LMDetailAppearView )
			);
			
			this.detailInfoNavigator.addScreen(
				LMonsterDetailNavigatorScreenName.SIZE,
				new ScreenNavigatorItem( LMDetailSizeView )
			);
			
			this.detailInfoNavigator.addScreen(
				LMonsterDetailNavigatorScreenName.ATTACK,
				new ScreenNavigatorItem( LMDetailAttackView )
			);
			
			this.detailInfoNavigator.addScreen(
				LMonsterDetailNavigatorScreenName.VIDEO,
				new ScreenNavigatorItem( LMDetailVideoView )
			);
			
			this._transitManager = new AnimationNaviTransitionManager( detailInfoNavigator );
			this.detailInfoNavigator.showScreen( LMonsterDetailNavigatorScreenName.NONE );
		
			addListener();
		}
		
		/**
		 * 绘制函数 
		 * 
		 */		
		override protected function draw():void
		{
			var minColumnNum:int = int(
				( UISize.MAIN_W / this.dpiScale - UISize.TAB_BUTTON_INDENT * 2 + 20 ) /
				( UISize.LMONSTER_DETAIL_TBUTTON_W + 20 )
			);
			var maxColumnNum:int = minColumnNum + 1;
			var minTabGap:Number = 
				( UISize.MAIN_W / this.dpiScale - UISize.TAB_BUTTON_INDENT * 2 - maxColumnNum * UISize.LMONSTER_DETAIL_TBUTTON_W ) / 
				minColumnNum;
			var totalNum:Number = buttonVec.length;
			
			if ( minTabGap >= 5 )
			{
				_tabColumnNum = maxColumnNum;
				_tabGap = minTabGap;
			}
			else
			{
				_tabColumnNum = minColumnNum;
				_tabGap = ( UISize.MAIN_W / this.dpiScale - UISize.TAB_BUTTON_INDENT * 2 - minColumnNum * UISize.LMONSTER_DETAIL_TBUTTON_W ) / 
					( minColumnNum - 1 );
			}
			
			var tmpButton:Radio;
			var columnPos:int;
			var rowPos:int;
			
			for ( var i:int = 0; i < totalNum; i ++ )
			{
				tmpButton = buttonVec[ i ] as Radio;
				columnPos = ( i % _tabColumnNum );
				rowPos = int( i / _tabColumnNum );
				tmpButton.x = ( UISize.TAB_BUTTON_INDENT + 
					columnPos * ( UISize.LMONSTER_DETAIL_TBUTTON_W + _tabGap ) ) * this.dpiScale;
				tmpButton.y = UISize.MAIN_H - ( UISize.LMONSTER_DETAIL_TAB_AREA_H + UISize.MONSTER_TAB_AREA_H - 20 ) * this.dpiScale +
					( UISize.TAB_BUTTON_H + 20 ) * rowPos * this.dpiScale;
			}
		}
		
		
		/**
		 * 转换导航栏页面 
		 * @param screenId 页面id
		 * 
		 */		
		public function changeNavigator( screenId:String ):void
		{
			detailInfoNavigator.showScreen( screenId );
		}
		
		/**
		 * 清除导航栏 
		 * 
		 */		
		public function clearNavi():void
		{
			_transitManager.clear();
			detailInfoNavigator.removeAllScreens();
		}
		
		public function showFirstScreen():void
		{
			changeNavigator( LMonsterDetailNavigatorScreenName.INTRODUCTION );
			_toggleGroup.selectedIndex = 0;
		}
	
		/**
		 * 添加侦听 
		 * 
		 */		
		private function addListener():void
		{
			_toggleGroup.addEventListener( Event.CHANGE, radioGroupChangeHandler );
		}
		
		//-----------------------------
		// events
		//-----------------------------
		
		/**
		 * 处理raidoGroup按钮点击后派发的事件
		 * @param event raidoGroup按钮点击后派发的事件
		 * 
		 */		
		public function radioGroupChangeHandler( event:Event ):void
		{
			switch(_toggleGroup.selectedIndex)
			{
				case 0:
				{
					changeNavigator( LMonsterDetailNavigatorScreenName.INTRODUCTION );	
					break;
				}
					
				case 1:
				{
					changeNavigator( LMonsterDetailNavigatorScreenName.MEAT_QUALITY );	
					break;
				}
					
				case 2:
				{
					changeNavigator( LMonsterDetailNavigatorScreenName.SP_EFFECT );	
					break;
				}
					
				case 3:
				{
					changeNavigator( LMonsterDetailNavigatorScreenName.ITEM_EFFECT );	
					break;
				}
					
				case 4:
				{
					changeNavigator( LMonsterDetailNavigatorScreenName.COLLECT );	
					break;
				}
					
				case 5:
				{
					changeNavigator( LMonsterDetailNavigatorScreenName.DESTROY );	
					break;
				}
					
				case 6:
				{
					changeNavigator( LMonsterDetailNavigatorScreenName.CATCH );	
					break;
				}
					
				case 7:
				{
					changeNavigator( LMonsterDetailNavigatorScreenName.APPEAR );	
					break;
				}
					
				case 8:
				{
					changeNavigator( LMonsterDetailNavigatorScreenName.SIZE );	
					break;
				}
					
				case 9:
				{
					changeNavigator( LMonsterDetailNavigatorScreenName.ATTACK );	
					break;
				}
					
				case 10:
				{
					changeNavigator( LMonsterDetailNavigatorScreenName.VIDEO );	
					break;
				}
					
				case 11:
				{
					changeNavigator( LMonsterDetailNavigatorScreenName.NONE );	
					
					dispatchCommonEvent(
						new LMonsterDetailViewEvent( LMonsterDetailViewEvent.BACK_TO_LIST )
					);
					break;
				}
					
				default:
				{
					break;
				}
			}
		}
		
		//-----------------------------
		// dispose
		//-----------------------------
		
		/**
		 * 析构函数 
		 * 
		 */		
		override public function dispose():void
		{
			this._toggleGroup.removeEventListeners();
			
			var len:int = buttonVec.length;
			
			for ( var i:int = 0; i < len; i ++ )
			{
				( buttonVec[ i ] as Radio ).dispose();
				FG2FUGenerator.deepDisposeButtonComp( buttonVec[ i ] );
				buttonVec[ i ] = null;
			}
			
			buttonVec.length = 0;
			buttonVec = null;
			_toggleGroup = null;
			detailInfoNavigator.dispose();
			detailInfoNavigator = null;
			_transitManager = null;
			super.dispose();
		}
		//-----------------------------
		// construction
		//-----------------------------
		
		/**
		 * 构造函数
		 *
		 */
		public function LMonsterDetailView()
		{
			super();
		}
	}
}